package com.typy.www.tpay.ui.itemview;

import android.content.Context;
import android.os.CountDownTimer;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.typy.www.tpay.R;
import com.typy.www.tpay.bean.bean.BaseReturn;
import com.typy.www.tpay.bean.vm.ActivicationVm;
import com.typy.www.tpay.constance.Constance;
import com.typy.www.tpay.constance.MessageEvent;
import com.typy.www.tpay.ui.acitivity.SuggestActivity;
import com.typy.www.tpay.ui.acitivity.WebActivity;
import com.typy.www.tpay.ui.view.CircleImageView;
import com.typy.www.tpay.ui.view.StarView;
import com.typy.www.tpay.util.ClassHoldUtil;
import com.typy.www.tpay.util.Logutil;
import com.typy.www.tpay.util.MessageAction;
import com.typy.www.tpay.util.ParamsUtil;
import com.typy.www.tpay.util.StringUtil;
import com.typy.www.tpay.util.UpdataUiUtil;
import com.typy.www.tpay.volley.HTTPUtils;
import com.typy.www.tpay.volley.VolleyListener;
import com.zhy.android.percent.support.PercentLinearLayout;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONException;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by 13799 on 2018/6/28.
 */

public class BuyCenterItemView {
    View mView;
    ViewHolder holder;
    private Context mContext;

    public BuyCenterItemView(Context context) {
        mContext = context;
        mView = LayoutInflater.from(context).inflate(R.layout.item_buyincenter, null);
        holder = new ViewHolder(mView);
    }

    public View getView() {
        return mView;
    }

    public void bind(final ActivicationVm vm, final int position, final int classType) {
        if (classType == ClassHoldUtil.BUYINCENTER)
            holder.tvSell.setText("购买");
        else if (classType == ClassHoldUtil.SELLOUTCENTER)
            holder.tvSell.setText("出售");
        else if (classType == ClassHoldUtil.BUYWEIWANCHENGC)
            holder.tvSell.setText("打款");
        else if (classType == ClassHoldUtil.SELLWEIWANCHENGC || classType == ClassHoldUtil.QUERENGDAKUANB) {
            holder.tvSell.setText("投诉");
        }
        else if (classType == ClassHoldUtil.QUERENGDAKUANS)
            holder.tvSell.setText("待确认收款");
        else if(classType==ClassHoldUtil.BUYYIWANCHENG||classType==ClassHoldUtil.SELLYIWANCHENG)
            holder.tvSell.setVisibility(View.GONE);
        if(!StringUtil.isEmpty(vm.getFdComplainId()))
            holder.tvSell.setText("投诉处理中");
        UpdataUiUtil.getDefaul().setText(vm.getFdNickName(), holder.tvNickname)
                .setText("交易余额：" + vm.getFdAmount() + "RMB", holder.tvTime)
                .setText("实付金额：" + vm.getFdMoney(), holder.tvPhone)
                .setText("支付方式：" + vm.getFdBankName(), holder.tvSharepoint);
        holder.tvSell.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(holder.tvSell.getText().toString().equals("投诉处理中")){
                    SuggestActivity.show(mContext, ClassHoldUtil.TOUSUCHULIZHONG, vm.getFdOrderId(),vm.getFdComplainId());
                }else if (classType == ClassHoldUtil.BUYINCENTER || classType == ClassHoldUtil.SELLOUTCENTER)
                    EventBus.getDefault().post(new MessageEvent(MessageAction.BUYCENTERDAILOG, position));
                else if (classType == ClassHoldUtil.BUYWEIWANCHENGC) {
                    SuggestActivity.show(mContext, ClassHoldUtil.DAKUAN, vm.getFdOrderId());
                } else if (classType == ClassHoldUtil.SELLWEIWANCHENGC || classType == ClassHoldUtil.QUERENGDAKUANB) {
                    SuggestActivity.show(mContext, ClassHoldUtil.TOUSU, vm.getFdOrderId());
                }else if (classType == ClassHoldUtil.QUERENGDAKUANS) {
                    SuggestActivity.show(mContext, ClassHoldUtil.DAIQUERENSHOUKUAN, vm.getFdOrderId());
                }

            }
        });
        holder.llTouxian.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(holder.tvSell.getText().toString().equals("投诉处理中")){
                    SuggestActivity.show(mContext, ClassHoldUtil.TOUSUCHULIZHONG, vm.getFdOrderId(),vm.getFdComplainId());
                }else {
                    if(classType!=ClassHoldUtil.BUYINCENTER&&classType!=ClassHoldUtil.SELLOUTCENTER)
                    getOrderdetailurlURL(vm.getFdOrderId());
                }
            }
        });
    }
    private void getOrderdetailurlURL(String orderId) {
        ParamsUtil params = new ParamsUtil();
        params.put("fdOrderId", orderId);
        HTTPUtils.post(mContext, Constance.getOrderdetailurlURL(), params.getParams(), new VolleyListener() {
            @Override
            public void onResponse(BaseReturn ret) {
                try {
                    String detailUrl = ret.getData().getString("data");
                    WebActivity.show(mContext,"订单详情",detailUrl);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onResponseErr(int code, String msg) {
            }
        });
    }


    static class ViewHolder {
        @BindView(R.id.img_touxian)
        CircleImageView imgTouxian;
        @BindView(R.id.tv_nickname)
        TextView tvNickname;
        @BindView(R.id.tv_time)
        TextView tvTime;
        @BindView(R.id.tv_uid)
        TextView tvUid;
        @BindView(R.id.stv_xinyu)
        StarView stvXinyu;
        @BindView(R.id.tv_phone)
        TextView tvPhone;
        @BindView(R.id.tv_sharepoint)
        TextView tvSharepoint;
        @BindView(R.id.tv_sell)
        TextView tvSell;
        @BindView(R.id.ll_share)
        PercentLinearLayout llShare;
        @BindView(R.id.img_div)
        ImageView imgDiv;
        @BindView(R.id.ll_touxian)
        PercentLinearLayout llTouxian;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
